package com.time.workshop.ui;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.time.workshop.API;
import com.time.workshop.App;
import com.time.workshop.R;
import com.time.workshop.SuperActivity;
import com.time.workshop.adapter.AddressAdapter;
import com.time.workshop.bean.CountyList;
import com.time.workshop.injector.Injector;
import com.time.workshop.injector.V;
import com.time.workshop.utils.CommonUtils;
import com.time.workshop.utils.Log;
import com.time.workshop.utils.NewToast;
/*
 * 区县
 */
public class AddressCountyActivity extends SuperActivity implements OnClickListener{
	private ListView xlistView;
	@V
	private TextView addrTextView;
	@V
	private ImageView img_login_back;
	
	private List<CountyList> countyList = new ArrayList<CountyList>();//区县
	private List<CountyList> streetList = new ArrayList<CountyList>();//街道
	private AddressAdapter madapter;
	
	public static AddressCountyActivity addrCountyActivity;
	@SuppressWarnings("unchecked")
	@Override
	protected void onCreate(Bundle bundle) {
		super.onCreate(bundle);
		setContentView(R.layout.activity_addr_country);
		
		addrCountyActivity = this;
		
		countyList = (List<CountyList>)getIntent().getSerializableExtra("countyList");
		init();
	}
	private void init() {
		Injector.getInstance().inject(this);
		img_login_back.setOnClickListener(this);
		addrTextView.setText(getResources().getString(R.string.city_name));
		
		xlistView = (ListView) findViewById(R.id.xlistView);

		madapter = new AddressAdapter(this,countyList);
		xlistView.setAdapter(madapter);
		xlistView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				App.county_name = countyList.get(position).getNAME();
				App.county_id = countyList.get(position).getID();
				App.street_id = "";
				App.street_name = "";
				reqStreet(countyList.get(position).getID(),countyList.get(position).getNAME());
			
			}
		});
	
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.img_login_back:
			back();
			break;
		default:
			break;
		}
	}	
	protected void reqStreet(String county_id, final String county_name) {
		if (!CommonUtils.isNetworkConnected(getApplicationContext())) {
			NewToast.makeText(getApplicationContext(), getResources().getString(R.string.M000));
			return;
		}
		showDialog(null);
		RequestParams params = new RequestParams();
		params.addQueryStringParameter("pid", county_id);

		HttpUtils httpUtils = new HttpUtils();

		httpUtils.send(HttpMethod.GET, API.areaList,params,new RequestCallBack<String>() {

			@Override 
			public void onSuccess(ResponseInfo<String> responseInfo) {
				Log.d("onSuccess==", responseInfo.result+"");

				dismissLoadingDialog();
				if (responseInfo.result.length() != 0) {


					try {
						JSONArray jsonObject = new JSONArray(responseInfo.result);

						Gson gson = new Gson();
						System.out.println("jsonObject.toString()::"+jsonObject.toString());

						streetList = gson.fromJson(jsonObject.toString(), new TypeToken<List<CountyList>>() {
						}.getType());

						if (streetList.size() != 0) {
							Intent intent = new Intent(AddressCountyActivity.this,AddressStreetActivity.class);
							intent.putExtra("streetList", (Serializable)streetList);
							intent.putExtra("county_name", county_name);
							startActivity(intent);
						}else {
							back();
						}
						
					} catch (JSONException e) {
						e.printStackTrace();
					}
				}
			}

			@Override
			public void onFailure(HttpException error, String msg) {
				dismissLoadingDialog();
				Log.d("onFailure==", msg+"");
				NewToast.makeText(getApplicationContext(), R.string.get_country_failure);
			}
		});
	
	}
}
